package cn.stazxr;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * @author talentestors
 * @version 1.0
 * @since 2025.05.19
 **/
public class JdbcUtil {

	public static Connection getConnection() {
		Connection conn = null;
		try {
			Properties properties = new Properties();
			properties.load(JdbcTest.class.getClassLoader().getResourceAsStream("jdbc.properties"));
			String url = properties.getProperty("mysql.url");
			String user = properties.getProperty("mysql.username");
			String password = properties.getProperty("mysql.password");
			conn = DriverManager.getConnection(url, user, password);
		} catch (IOException | SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}

	public static void closeAll(Connection conn, Statement statement, ResultSet resultSet) {
		if (resultSet != null) {
			try {
				resultSet.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (statement != null) {
			try {
				statement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

}
